package com.sfzd5.mistakesbook;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO: document your custom view class.
 */
public class SelectRecyclerView extends LinearLayout {
    Context context;
    View root;
    private String mTitle;
    private int mSelectedColor = Color.RED;
    private ArrayList mList;
    private ArrayList mSelected;
    private AddNewItemInterface addNewItemInterface;

    public List<Object> getSelected(){
        return mSelected;
    }

    public void setSingleSelect(boolean singleSelect) {
        isSingleSelect = singleSelect;
    }

    private boolean isSingleSelect = false;

    private TextView tv_title;
    private ImageButton ib_add;
    private RecyclerView cv_list;
    private RecyclerView cv_selected;

    public SelectRecyclerView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public SelectRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public SelectRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        this.context = context;
        root = LayoutInflater.from(context).inflate(R.layout.select_recycler_view, SelectRecyclerView.this);
        ib_add = root.findViewById(R.id.ib_add);
        cv_list = root.findViewById(R.id.cv_list);
        cv_selected = root.findViewById(R.id.cv_selected);
        tv_title = root.findViewById(R.id.tv_title);
    }

    public void addNewObject(Object obj){
        mList.add(obj);
        if(isSingleSelect) mSelected.clear();
        mSelected.add(obj);
        cv_list.getAdapter().notifyDataSetChanged();
        cv_selected.getAdapter().notifyDataSetChanged();
    }

    public void setVal(String title, ArrayList list, ArrayList selected, AddNewItemInterface addNewItemInterface){
        this.mTitle = title;
        this.mList = list;
        this.mSelected = selected;
        this.addNewItemInterface = addNewItemInterface;

        tv_title.setText(this.mTitle);

        if(this.mSelected == null)
            mSelected = new ArrayList<>();

        LinearLayoutManager cv_list_linearLayoutManager = new LinearLayoutManager(context);
        cv_list_linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        cv_list.setLayoutManager(cv_list_linearLayoutManager);
        cv_list.setAdapter(new SelectRecyclerViewAdapter(this.mList, context));

        LinearLayoutManager cv_selected_linearLayoutManager = new LinearLayoutManager(context);
        cv_selected_linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        cv_selected.setLayoutManager(cv_selected_linearLayoutManager);
        cv_selected.setAdapter(new SelectRecyclerViewAdapter(this.mSelected, context));

        ib_add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                addNewItemInterface.addNewItem();
            }
        });
    }

    class SelectRecyclerViewAdapter extends RecyclerView.Adapter<TextViewViewHolder>{
        private List<Object> datas;
        Context context;
        LayoutInflater inflater;
        public SelectRecyclerViewAdapter(List<Object> datas, Context context){
            this.datas = datas;
            this.context = context;
            this.inflater = LayoutInflater.from(context);
        }

        @NonNull
        @Override
        public TextViewViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = inflater.inflate(R.layout.select_recycler_item, parent, false);
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    Object obj = view.getTag();
                    if(contains(mSelected, obj)){
                        mSelected.remove(obj);
                    } else {
                        if(isSingleSelect) mSelected.clear();
                        mSelected.add(obj);
                    }
                    cv_list.getAdapter().notifyDataSetChanged();
                    cv_selected.getAdapter().notifyDataSetChanged();
                }
            });
            return new TextViewViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull TextViewViewHolder holder, int position) {
            Object obj = datas.get(position);
            holder.tv.setText(obj.toString());
            holder.tv.setTag(obj);
            if(contains(mSelected, obj)){
                holder.tv.setTextColor(mSelectedColor);
            } else {
                holder.tv.setTextColor(Color.BLACK);
            }
        }

        @Override
        public int getItemCount() {
            return datas.size();
        }

        boolean contains(List<Object> objs, Object obj) {
            String v = obj.toString();
            for (Object o : objs) {
                if (v.equals(o.toString())) {
                    return true;
                }
            }
            return false;
        }
    }
}